package algorithm.easy;

public class Solution110 {

    //输入：root = [3,9,20,null,null,15,7]
    //输出：true
    //输入：root = [1,2,2,3,3,null,null,4,4]
    //输出：false
    public static  boolean isBalanced(TreeNode root) {
        if(root ==null){
            return true;
        }

        return (Math.abs(maxDepth(root.left)-maxDepth(root.right)) <=1) //左右子树高度小于等于1
                && isBalanced(root.left)                                //左子树为平衡树
                && isBalanced(root.right);                              //右子树为平衡树
    }

    public static int maxDepth(TreeNode root){
       if(root==null){
           return 0;
       }
        return Math.max(maxDepth(root.left),maxDepth(root.right)) +1;
    }

    public static void main(String[] args){
            TreeNode treeNode = TreeNode.init101();
            System.out.println(isBalanced(treeNode));
    }

}
